package com.innovation.util;

import java.text.MessageFormat;

import com.innovation.config.MessageConfig;
import com.innovation.message.ErrorMsgProto.ErrorMsg;
import com.innovation.message.PBMessageProto.PBMessage;
import com.innovation.message.ServerMessageProto.ServerMessage;

/**
 * 协议工具类
 * @author Administrator
 *
 */
public class MessageUtil {
	
	/**
	 * 构建协议信息
	 * @param playerId
	 * @param actCode
	 * @return
	 */
	public static PBMessage.Builder buildMessage(int playerId, int actCode){
		PBMessage.Builder builder = PBMessage.newBuilder();
		builder.setPlayerId(playerId);
		builder.setCode(actCode);
		builder.setSequence(0);
		builder.setErrorCode(0);
		return builder;
	}
	
	/**
	 * 构建错误信息
	 * @param errorCode
	 * @param args
	 * @return
	 */
	public static ErrorMsg buildErrorMsg(int errorCode, Object... args){
		return buildErrorMsg(errorCode, "", args);
	}
	
	/**
	 * 构建错误信息
	 * @param errorCode
	 * @param extraData 附加数据信息
	 * @param args
	 * @return
	 */
	public static ErrorMsg buildErrorMsg(int errorCode, String extraData, Object... args){
		ErrorMsg.Builder builder = ErrorMsg.newBuilder();
		builder.setCode(errorCode);
		builder.setExtraData(extraData);
		String msg = MessageConfig.getErrorMessage(errorCode);
		msg = MessageFormat.format(msg, args);
		builder.setText(msg);
		return builder.build();
	}
	
	/**构建服务器之间传输的消息协议头
	 * @param serverId 请求发送到的服务器id标示
	 * @param playerId
	 * @param cmdCode
	 * @param isForward
	 * @param forwardId
	 * @return
	 */
	public static ServerMessage.Builder buildServerMsg(int serverId, int playerId, int cmdCode, boolean isForward, int forwardId){
		ServerMessage.Builder builder = ServerMessage.newBuilder();
		builder.setServerId(serverId);
		builder.setPlayerId(playerId);
		builder.setForwardId(forwardId);
		builder.setCmdCode(cmdCode);
		builder.setIsForward(isForward);
		return builder;
	}
	
	/**
	 * 构建服务器之间传输的消息协议头
	 * @param id
	 * @param cmdCode
	 * @return
	 */
	public static ServerMessage.Builder buildServerMsg(int serverId, int playerId, int cmdCode){
		return buildServerMsg(serverId, playerId, cmdCode, false, 0);
	}
}
